@include reader.awk
@include misc.awk
@include args2opt

BEGIN  { OFS = FS = ","; 
         args2opt("seed=1;target=1;noise=10",Opt) 
         seed(Opt["seed"]) 
       }
line() { sub(/^=/,"") ? header(Db) : row(Db) } 
END   { mutate(Db,
               Opt["noise"],
               (Opt["target"]==1 || Opt["target"]==3),
               (Opt["target"]==2 || Opt["target"]==3)) 
      }

function mutate(db,f,classp,otherp,  
                maxr,maxc,r,c,other) {
    maxr = db["rows"]
    maxc = db["cols"]
    for(r=2;r<=maxr;r++) 
        for(c=1;c<=maxc;c++) 
            if (rand() <= f) {
                other = 1 + anyi(maxr - 1) # dodge header row
                if (db["class"][c]) {
                    if(classp)  
                        db[r][c] = db[other][c]
                } else 
                    if (otherp)
                        db[r][c] = db[other][c] 
            }
    for(r=1;r<=maxr;r++) 
        print row2str(r,maxc)
}
function row2str(r,maxc,  out,c) {
    out = db[r][1]
    for(c=1;i<=maxc;c++)
        out = out OFS db[r][c] 
    return out
}
